//
//  BK_Calendar_ViewCell.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/2/28.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_Calendar_ViewCell.h"
#import "BK_Calendar_Model.h"
#import "NSCalendar+BKODCalendarExtensions.h"


#import "BK_OD_Color.h"




@implementation BK_Calendar_ViewCell
{
    UIView *_selectedAnimatView;
    UILabel *_titleLab;
    UILabel *_subTitleLab;
    NSLayoutConstraint *_titleLabCenterYConstraint;
    NSCalendar *_calendar;
    double _apartCenter;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    _apartCenter = CGRectGetHeight([UIScreen mainScreen].bounds) * 0.0134;
    _calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    self.contentView.backgroundColor = [UIColor clearColor];
    
    _selectedAnimatView = [[UIView alloc] init];
//    _selectedAnimatView.layer.cornerRadius = 4;
//    _selectedAnimatView.layer.masksToBounds = YES;
    [self.contentView addSubview:_selectedAnimatView];
    
    _titleLab = [[UILabel alloc] init];
    _titleLab.font = [UIFont systemFontOfSize:13];
    _titleLab.textColor = kOrderBlack1;
    _titleLab.textAlignment = NSTextAlignmentCenter;
    _titleLab.backgroundColor = [UIColor clearColor];
    _titleLab.numberOfLines = 1;
    [self.contentView addSubview:_titleLab];
    
    [self makeConstraints];
}

- (void)makeConstraints
{
    _selectedAnimatView.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    
    
    NSLayoutConstraint *selectedAnimatViewTopConstraint = [NSLayoutConstraint constraintWithItem:_selectedAnimatView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:1];
    
    
    NSLayoutConstraint *selectedAnimatViewLeftConstraint = [NSLayoutConstraint constraintWithItem:_selectedAnimatView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:1];
    
    
    NSLayoutConstraint *selectedAnimatViewRightConstraint = [NSLayoutConstraint constraintWithItem:_selectedAnimatView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1 constant:-1];
    
    
    NSLayoutConstraint *selectedAnimatViewBottomConstraint = [NSLayoutConstraint constraintWithItem:_selectedAnimatView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1 constant:-1];
    
    
    [self.contentView addConstraints:@[selectedAnimatViewTopConstraint, selectedAnimatViewLeftConstraint, selectedAnimatViewRightConstraint, selectedAnimatViewBottomConstraint]];
    
    _titleLab.translatesAutoresizingMaskIntoConstraints = NO;
//    _titleLabCenterYConstraint = [NSLayoutConstraint constraintWithItem:_titleLab attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:-0];
    
    _titleLabCenterYConstraint = [NSLayoutConstraint constraintWithItem:_titleLab attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:-_apartCenter];
    
    
    NSLayoutConstraint *titleLabCenterXConstraint = [NSLayoutConstraint constraintWithItem:_titleLab attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
    
    NSLayoutConstraint *titleLabWidthConstraint = [NSLayoutConstraint constraintWithItem:_titleLab attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
    
    [self.contentView addConstraints:@[titleLabCenterXConstraint, _titleLabCenterYConstraint, titleLabWidthConstraint]];
    
    
}

- (void)fillData:(id)data
{
    if (!data) {
        return;
    }
    BK_Calendar_Model *obj = data;
    NSString *title = obj.title ? obj.title : obj.date ? [NSString stringWithFormat:@"%ld",(long)[[_calendar od_dateComponents:obj.date] day]] : @"";
    NSString *subTitle = obj.subTitle ? obj.subTitle : @"";
    
    _titleLab.text = title;
    
    if (subTitle && ![subTitle isEqualToString:@""]) {
        if (!_subTitleLab) {
            
//            _titleLabCenterYConstraint.constant = -_apartCenter;
            
            _subTitleLab = [[UILabel alloc] init];
            _subTitleLab.font = [UIFont systemFontOfSize:11];
            _subTitleLab.textColor = kOrderPriceRed;
            _subTitleLab.text = subTitle;
            _subTitleLab.backgroundColor = [UIColor clearColor];
            _subTitleLab.textAlignment = NSTextAlignmentCenter;
            _subTitleLab.numberOfLines = 1;
            [self.contentView addSubview:_subTitleLab];
            
            _subTitleLab.translatesAutoresizingMaskIntoConstraints = NO;
            
            NSLayoutConstraint *subTitleLabCenterYConstraint = [NSLayoutConstraint constraintWithItem:_subTitleLab attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1 constant:_apartCenter];
            NSLayoutConstraint *subTitleLabCenterXConstraint = [NSLayoutConstraint constraintWithItem:_subTitleLab attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];
            
            NSLayoutConstraint *subTitleLabWidthConstraint = [NSLayoutConstraint constraintWithItem:_subTitleLab attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
            
            [self.contentView addConstraints:@[subTitleLabCenterYConstraint, subTitleLabCenterXConstraint, subTitleLabWidthConstraint]];
        }
        
    }
    else{
        if (_subTitleLab) {
            [_subTitleLab removeFromSuperview];
            _subTitleLab = nil;
//            _titleLabCenterYConstraint.constant = 0;
        }
    }
}

+ (NSInteger)computeNumberOfRows:(id)data
{
    if (!data) {
        return 0;
    }
    BK_Calendar_Model *obj = data;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *dateComponents = [calendar od_dateComponents:obj.date];
    NSInteger weekOfMonth = dateComponents.weekOfMonth;
    return weekOfMonth;
}

- (void)select1:(BOOL)animation{
    
    _titleLab.textColor = [UIColor whiteColor];
    _subTitleLab.textColor = [UIColor whiteColor];
    
    if (animation == NO) {
        _selectedAnimatView.backgroundColor = [UIColor colorWithRed:0.011 green:0.7 blue:0.59 alpha:1];
        return;
    }
    
    _selectedAnimatView.transform = CGAffineTransformMakeScale(0.3, 0.3);
    _selectedAnimatView.backgroundColor = [UIColor colorWithRed:0.011 green:0.7 blue:0.59 alpha:1];
    [UIView animateWithDuration:0.2 animations:^{
        _selectedAnimatView.transform = CGAffineTransformMakeScale(1.1, 1.1);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.1 animations:^{
            _selectedAnimatView.transform = CGAffineTransformMakeScale(1, 1);
        }];
    }];
}

- (void)unselect1:(BOOL)animation{
    
    _titleLab.textColor = kOrderBlack1;
    _subTitleLab.textColor = kOrderPriceRed;
    
    if (animation == NO) {
        _selectedAnimatView.backgroundColor = [UIColor clearColor];
        return;
    }
    
    [UIView animateWithDuration:0.2 animations:^{
        _selectedAnimatView.alpha = 0.1;
    } completion:^(BOOL finished) {
        _selectedAnimatView.alpha = 1;
        _selectedAnimatView.backgroundColor = [UIColor clearColor];
        
    }];
}
@end
